package com.cloud.wms.ui.print.entity;


import android.graphics.Bitmap;
import android.graphics.Point;
import android.os.Parcel;
import android.text.TextUtils;

import com.cloud.core.utils.QRCodeUtils;
import com.cloud.printer.FineExPrinter;
import com.cloud.printer.Printer;
import com.cloud.printer.jpl.Barcode;
import com.cloud.printer.jpl.Image;
import com.cloud.printer.jpl.JPL;
import com.cloud.printer.jpl.Page;
import com.cloud.printer.jpl.Text;
import com.cloud.wms.R;
import com.cloud.wms.app.FineExApplication;

import java.util.List;

/**
 * @Author： Chen
 * @Date： 2020/7/6
 * @Desc： 箱唛打印实体以及模板
 */
public class PrinterBoxEntity implements Printer {

    private String orderDate;//单据日期
    private String boxID;
    private String boxMarking;//箱唛标识
    private String boxCode;//箱唛号
    private String WarehouseMember;//仓库会员
    private String inCode;//入库单号
    private String syncId;//外部单号
    private String commodityName;//商品名称
    private String barCode;//条码
    private String amount;
    private String wareHouseName;//仓库名称
    private String memberName;//会员名称
//    private List<KeyValue> MarkConfig;//动态配置的

    private String MemberNo;//会员号
    private String BoxGauge;//箱规
    private String BatchCode;//批次编码
    private String shelfLife;//保质期
    private String tentativePeriod;//临保期

    private String Count;//数量
    private String remark;//备注
    private String printUser;//打印人
    private String printDate;//打印日期

    private boolean isSelect;

    public String getBoxID() {
        return boxID;
    }

    public void setBoxID(String boxID) {
        this.boxID = boxID;
    }

    public String getBoxMarking() {
        return boxMarking;
    }

    public void setBoxMarking(String boxMarking) {
        this.boxMarking = boxMarking;
    }

    public String getBoxCode() {
        return boxCode;
    }

    public void setBoxCode(String boxCode) {
        this.boxCode = boxCode;
    }

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    public String getWareHouseName() {
        return wareHouseName;
    }

    public void setWareHouseName(String wareHouseName) {
        this.wareHouseName = wareHouseName;
    }

    public String getMemberName() {
        return memberName;
    }

    public void setMemberName(String memberName) {
        this.memberName = memberName;
    }

    public boolean isSelect() {
        return isSelect;
    }

    public void setSelect(boolean select) {
        isSelect = select;
    }

    public String getBoxMark() {
        if (TextUtils.isEmpty(boxCode)) return "";
        return boxCode;
    }

    public void setBoxMark(String boxMark) {
        boxCode = boxMark;
    }

    public String getWarehouseMember() {
        if (TextUtils.isEmpty(WarehouseMember)) return "";
        return getWareHouseName() + "-" + getMemberName();
    }

    public void setWarehouseMember(String warehouseMember) {
        WarehouseMember = warehouseMember;
    }

    public String getInCode() {
        if (TextUtils.isEmpty(inCode)) return "";
        return inCode;
    }

    public void setInCode(String inCode) {
        inCode = inCode;
    }

    public String getSyncId() {
        if (TextUtils.isEmpty(syncId)) return "";
        return syncId;
    }

    public void setSyncId(String syncId) {
        syncId = syncId;
    }

    public String getCommodityName() {
        if (TextUtils.isEmpty(commodityName)) return "";
        return commodityName;
    }

    public void setCommodityName(String commodityName) {
        commodityName = commodityName;
    }

    public String getBarCode() {
        if (TextUtils.isEmpty(barCode)) return "";
        return barCode;
    }

    public void setBarCode(String barCode) {
        barCode = barCode;
    }

//    public List<KeyValue> getMarkConfig() {
//        return MarkConfig;
//    }
//
//    public void setMarkConfig(List<KeyValue> markConfig) {
//        MarkConfig = markConfig;
//    }

    public String getMemberNo() {
        if (TextUtils.isEmpty(MemberNo)) return "";
        return MemberNo;
    }

    public void setMemberNo(String memberNo) {
        MemberNo = memberNo;
    }

    public String getBoxGauge() {
        if (TextUtils.isEmpty(BoxGauge)) return "";
        return BoxGauge;
    }

    public void setBoxGauge(String boxGauge) {
        BoxGauge = boxGauge;
    }

    public String getBatchCode() {
        if (TextUtils.isEmpty(BatchCode)) return "";
        return BatchCode;
    }

    public void setBatchCode(String batchCode) {
        BatchCode = batchCode;
    }

    public String getShelfLife() {
        if (TextUtils.isEmpty(shelfLife)) return "";
        return shelfLife;
    }

    public void setShelfLife(String shelfLife) {
        shelfLife = shelfLife;
    }

    public String getTentativePeriod() {
        if (TextUtils.isEmpty(tentativePeriod)) return "";
        return tentativePeriod;
    }

    public void setTentativePeriod(String tentativePeriod) {
        tentativePeriod = tentativePeriod;
    }

    public String getCount() {
        if (TextUtils.isEmpty(Count)) return "";
        return Count;
    }

    public void setCount(String count) {
        Count = count;
    }

    public String getRemark() {
        if (TextUtils.isEmpty(remark)) return "";
        return remark;
    }

    public void setRemark(String Remark) {
        this.remark = Remark;
    }

    public String getPrintUser() {
        if (TextUtils.isEmpty(printUser)) return "";
        return printUser;
    }

    public void setPrintUser(String PrintUser) {
        this.printUser = PrintUser;
    }

    public String getPrintDate() {
        if (TextUtils.isEmpty(printDate)) return "";
        return printDate;
    }

    public void setPrintDate(String PrintDate) {
        this.printDate = PrintDate;
    }

    public String getOrderDate() {
        if (TextUtils.isEmpty(orderDate)) return "";
        return orderDate;
    }

    public void setOrderDate(String OrderDate) {
        this.orderDate = OrderDate;
    }

    public void print() {

    }

    public PrinterBoxEntity() {
    }

    public static final int PRINT_WIDTH = 556;
    public static final int PRINT_HEIGHT = 972;

    @Override
    public void print(FineExPrinter printer) {
        int fontSize = 22;
        int lineWidth = 2;
        int fontHeight = 36;

        printer.jpl.page.start(0, 0, PRINT_WIDTH, PRINT_HEIGHT, Page.PAGE_ROTATE.x0);
        printer.jpl.image.drawOut(6, 0, FineExApplication.component().resources(), R.mipmap.fineex_print_logo, Image.IMAGE_ROTATE.ANGLE_0);

        printer.jpl.text.drawOut(FineExPrinter.ALIGN.RIGHT, 8, "单据日期：" + getOrderDate(), 22, false, false, false, false, Text.TEXT_ENLARGE.x1, Text.TEXT_ENLARGE.x1, JPL.ROTATE.ROTATE_0);

        //打印区域横线
        /**
         *   ***************
         *   *             *
         *   *             *
         *   *             *
         *   *             *
         *   ***************
         */
        //横线
        printer.jpl.graphic.line(new Point(6, 40), new Point(PRINT_WIDTH, 40), lineWidth);
        printer.jpl.graphic.line(new Point(6, PRINT_HEIGHT - 60), new Point(PRINT_WIDTH, PRINT_HEIGHT - 60), lineWidth);
        //竖线
        printer.jpl.graphic.line(new Point(6, 40), new Point(6, PRINT_HEIGHT - 60), lineWidth);
        printer.jpl.graphic.line(new Point(PRINT_WIDTH, 40), new Point(PRINT_WIDTH, PRINT_HEIGHT - 60), lineWidth);

        //入库箱号及其条码
        printer.jpl.text.drawOut(12, 69, "箱号：", 22);
        printer.jpl.barcode.code128(FineExPrinter.ALIGN.CENTER, 50, 50, Barcode.BAR_UNIT.x2, Barcode.BAR_ROTATE.ANGLE_0, getBoxMark());
        printer.jpl.text.drawOut(FineExPrinter.ALIGN.CENTER, 106, getBoxMark(), 22, false, false, false, false, Text.TEXT_ENLARGE.x1, Text.TEXT_ENLARGE.x1, JPL.ROTATE.ROTATE_0);
        printer.jpl.graphic.line(new Point(6, 140), new Point(PRINT_WIDTH, 140), lineWidth);

        //仓库信息
        printer.jpl.text.drawOut(12, textStartY(160, fontHeight, 1), "仓库会员：" + getWarehouseMember(), fontSize, false);
        printer.jpl.text.drawOut(12, textStartY(160, fontHeight, 2), "入库单号：" + getInCode(), fontSize, false);
        printer.jpl.text.drawOut(12, textStartY(160, fontHeight, 3), "外部单号：" + getSyncId(), fontSize, false);
        printer.jpl.text.drawOut(12, textStartY(160, fontHeight, 4), "商品名称：" + getCommodityName(), fontSize, false);
        printer.jpl.graphic.line(new Point(6, 320), new Point(PRINT_WIDTH, 320), lineWidth);

        //配置信息
//        for (int i = 0; i < getMarkConfig().size(); i++) {
//            KeyValue value = getMarkConfig().get(i);
//            printer.jpl.text.drawOut(12, textStartY(330, fontHeight, i + 1), value.getKey() + "：" + value.getValue(), fontSize, false);
//        }
        printer.jpl.graphic.line(new Point(6, 550), new Point(PRINT_WIDTH, 550), lineWidth);


        printer.jpl.text.drawOut(12, 600, "商品条码：", 22);
        printer.jpl.barcode.code128(FineExPrinter.ALIGN.CENTER, 570, 50, Barcode.BAR_UNIT.x2, Barcode.BAR_ROTATE.ANGLE_0, getBarCode());
        printer.jpl.text.drawOut(FineExPrinter.ALIGN.CENTER, 630, getBarCode(), 22, false, false, false, false, Text.TEXT_ENLARGE.x1, Text.TEXT_ENLARGE.x1, JPL.ROTATE.ROTATE_0);
        printer.jpl.graphic.line(new Point(6, 670), new Point(PRINT_WIDTH, 670), lineWidth);

        printer.jpl.text.drawOut(12, textStartY(680, fontHeight, 1), "数量：", fontSize, false);
        printer.jpl.text.drawOut(12, textStartY(680, fontHeight, 2), getCount(), fontSize, true);

        printer.jpl.graphic.line(new Point(100, 670), new Point(100, PRINT_HEIGHT - 60), lineWidth);
        printer.jpl.graphic.line(new Point(PRINT_WIDTH - 250, 670), new Point(PRINT_WIDTH - 250, PRINT_HEIGHT - 60), lineWidth);

        String remark = "备注：" + getRemark();
        int remarkLength = remark.length();
        if (remarkLength <= 11) {
            printer.jpl.text.drawOut(106, textStartY(680, fontHeight, 1), remark, fontSize);
        } else if (remarkLength <= 22) {
            printer.jpl.text.drawOut(106, textStartY(680, fontHeight, 1), remark.substring(0, 11), fontSize);
            printer.jpl.text.drawOut(106, textStartY(680, fontHeight, 2), remark.substring(11), fontSize);
        } else if (remarkLength <= 33) {
            printer.jpl.text.drawOut(106, textStartY(680, fontHeight, 1), remark.substring(0, 11), fontSize);
            printer.jpl.text.drawOut(106, textStartY(680, fontHeight, 2), remark.substring(11, 22), fontSize);
            printer.jpl.text.drawOut(106, textStartY(680, fontHeight, 3), remark.substring(22), fontSize);
        } else {
            printer.jpl.text.drawOut(106, textStartY(680, fontHeight, 1), remark.substring(0, 11), fontSize);
            printer.jpl.text.drawOut(106, textStartY(680, fontHeight, 2), remark.substring(11, 22), fontSize);
            printer.jpl.text.drawOut(106, textStartY(680, fontHeight, 3), remark.substring(22, 32) + "…", fontSize);
        }

        String text = getBoxMark() + "/" + getMemberNo() + "/" + getInCode() + "/" + getBoxGauge() + "/" + getBatchCode() + "/" + getShelfLife() + "/" + getTentativePeriod();
        Bitmap qrImage = QRCodeUtils.createQRCode(text, 230);
        printer.jpl.image.drawOut(PRINT_WIDTH - 240, 680, 230, 230, printer.jpl.image.CovertImageHorizontal(qrImage, 128), false, Image.IMAGE_ROTATE.ANGLE_0, 0, 0);

        printer.jpl.text.drawOut(12, textStartY(PRINT_HEIGHT - 50, fontHeight, 1), "打印人：" + getPrintUser(), fontSize);
        printer.jpl.text.drawOut(FineExPrinter.ALIGN.RIGHT, textStartY(PRINT_HEIGHT - 50, fontHeight, 1), "打印日期：" + getPrintDate(),
                fontSize, false, false, false, false, Text.TEXT_ENLARGE.x1, Text.TEXT_ENLARGE.x1, JPL.ROTATE.ROTATE_0);

        printer.jpl.page.end();
        printer.jpl.page.print();
        printer.jpl.feedMarkOrGap(0);//printer.jpl.feedNextLabelEnd(48);
    }

    private int textStartY(int startY, int fontHeight, int line) {
        if (startY < 0)
            return 0;
        if (fontHeight < 0)
            return 0;
        if (line < 1)
            return 0;
        return startY + fontHeight * (line - 1);
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.orderDate);
        dest.writeString(this.boxID);
        dest.writeString(this.boxMarking);
        dest.writeString(this.boxCode);
        dest.writeString(this.WarehouseMember);
        dest.writeString(this.inCode);
        dest.writeString(this.syncId);
        dest.writeString(this.commodityName);
        dest.writeString(this.barCode);
        dest.writeString(this.amount);
        dest.writeString(this.wareHouseName);
        dest.writeString(this.memberName);
//        dest.writeTypedList(this.MarkConfig);
        dest.writeString(this.MemberNo);
        dest.writeString(this.BoxGauge);
        dest.writeString(this.BatchCode);
        dest.writeString(this.shelfLife);
        dest.writeString(this.tentativePeriod);
        dest.writeString(this.Count);
        dest.writeString(this.remark);
        dest.writeString(this.printUser);
        dest.writeString(this.printDate);
    }

    protected PrinterBoxEntity(Parcel in) {
        this.orderDate = in.readString();
        this.boxID = in.readString();
        this.boxMarking = in.readString();
        this.boxCode = in.readString();
        this.WarehouseMember = in.readString();
        this.inCode = in.readString();
        this.syncId = in.readString();
        this.commodityName = in.readString();
        this.barCode = in.readString();
        this.amount = in.readString();
        this.wareHouseName = in.readString();
        this.memberName = in.readString();
//        this.MarkConfig = in.createTypedArrayList(KeyValue.CREATOR);
        this.MemberNo = in.readString();
        this.BoxGauge = in.readString();
        this.BatchCode = in.readString();
        this.shelfLife = in.readString();
        this.tentativePeriod = in.readString();
        this.Count = in.readString();
        this.remark = in.readString();
        this.printUser = in.readString();
        this.printDate = in.readString();
    }

    public static final Creator<PrinterBoxEntity> CREATOR = new Creator<PrinterBoxEntity>() {
        @Override
        public PrinterBoxEntity createFromParcel(Parcel source) {
            return new PrinterBoxEntity(source);
        }

        @Override
        public PrinterBoxEntity[] newArray(int size) {
            return new PrinterBoxEntity[size];
        }
    };
}
